//
//  RootTabBarController.m
//  HeartTravel
//
//  Created by lanou on 15/10/24.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "RootTabBarController.h"
#import "HTIndexViewController.h"
#import "HTGroupViewController.h"
#import "HTGuideViewController.h"
#import "HTMineViewController.h"
#import "HTTravelViewController.h"
#import "HTSingleTon.h"
@interface RootTabBarController ()

@property (nonatomic, strong) UIButton *guideButton;

@end

@implementation RootTabBarController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self p_setupControllers];
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(SCREEN_WIDTH/2 - 20, 5, 40, 40);
//    [button setBackgroundImage:[UIImage imageNamed:@"nav_guide"] forState:(UIControlStateNormal)];
    button.backgroundColor = [UIColor colorWithRed:0.213 green:0.450 blue:0.970 alpha:1.000];
    button.layer.cornerRadius = 10;
    button.layer.masksToBounds = YES;
    [button setTitle:@"➕" forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(handleGuideButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.tabBar addSubview:button];
}

// 添加controller
- (void)p_setupControllers {
    
    // 五个模块: 首页、社区、指导、动态、我的
    
    // 首页
    HTIndexViewController *indexVC = [[HTIndexViewController alloc] init];
    UINavigationController *indexNC = [[UINavigationController alloc] initWithRootViewController:indexVC];
    indexVC.navigationController = indexNC;
    
    [indexNC.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav"] forBarMetrics:(UIBarMetricsDefault)];
    indexNC.tabBarItem.title = @"首页";
    indexNC.tabBarItem.image = [UIImage imageNamed:@"nav_index_nor"];
    
    // 社区
    HTGroupViewController *groupVC = [[HTGroupViewController alloc] init];
    UINavigationController *groupNC = [[UINavigationController alloc] initWithRootViewController:groupVC];
    
    [groupNC.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav"] forBarMetrics:(UIBarMetricsDefault)];
    groupNC.tabBarItem.title = @"攻略";
    groupNC.tabBarItem.image = [UIImage imageNamed:@"导游"];
    
    // 指导
    HTGuideViewController *guideVC = [[HTGuideViewController alloc] init];
    UINavigationController *guideNC = [[UINavigationController alloc] initWithRootViewController:guideVC];
    
    [guideNC.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav"] forBarMetrics:(UIBarMetricsDefault)];
    guideNC.tabBarItem.title = @"";
    guideNC.tabBarItem.image = [UIImage imageNamed:@"nav_guide_32"];
    
    
    //游记
    HTTravelViewController *travelVC = [[HTTravelViewController alloc] init];
    UINavigationController *travelNC = [[UINavigationController alloc] initWithRootViewController:travelVC];
    [travelNC.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav"] forBarMetrics:(UIBarMetricsDefault)];
    travelNC.tabBarItem.title = @"游记";
    travelNC.tabBarItem.image = [UIImage imageNamed:@"nav_travel_nor"];
    

    
    // 我的
    HTMineViewController *mineVC = [[HTMineViewController alloc] init];
    UINavigationController *mineNC = [[UINavigationController alloc] initWithRootViewController:mineVC];
    
    [mineNC.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav"] forBarMetrics:(UIBarMetricsDefault)];
    mineNC.tabBarItem.title = @"我的";
    mineNC.tabBarItem.image = [UIImage imageNamed:@"nav_mine_nor"];
    
    self.viewControllers = @[indexNC, groupNC, guideNC, travelNC, mineNC];
    
    
    //给单例添加观察者,当观察者发现isOn值改变时改变所有界面的颜色
//    HTSingleTon *singleton = [[HTSingleTon alloc]init];
//    [singleton addObserver:self forKeyPath:@"isOn" options:(NSKeyValueObservingOptionNew) context:nil];

    
}

//- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
//{
//    for (UIViewController *vc in self.viewControllers) {
//        if ([(HTSingleTon *)object isOn]) {
//            vc.view.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.500];
//        }else{
//            
//            vc.view.backgroundColor = [UIColor whiteColor];
//        }
//    }
//    
//    //[object removeObserver:self forKeyPath:@"isOn" context:nil];
//    
//}





- (void)handleGuideButtonAction:(id)sender {
    
    HTGuideViewController *guideVC = [[HTGuideViewController alloc]init];
    [self presentViewController:guideVC animated:YES completion:^{
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
